package com.excalibur.core.mvc.editor;

import java.beans.PropertyEditorSupport;

import com.excalibur.core.util.CheckStyleUtil;
import com.excalibur.core.util.xss.HtmlCleaner;
import org.springframework.web.util.HtmlUtils;

public class StringHtmlEscapeEditor extends PropertyEditorSupport{
	
	public void setAsText(String text) 
	{
		if (text == null) 
		{
			setValue(null);
		}
		else 
		{
			String value = text.trim();
			// 不是URL的时候，做编码
			if(CheckStyleUtil.checkStyle(value, CheckStyleUtil.PATTERN_URL)){
				setValue(HtmlCleaner.cleanScriptStyle(value));
			} else {
				setValue(HtmlUtils.htmlEscape(HtmlCleaner.cleanScriptStyle(value)));
			}
		}
	}

	public String getAsText() 
	{
		Object value = getValue();
		return (value != null ? value.toString() : "");
	}

}
